<?php
namespace App\Http\Controllers;
use App\Exceptions\ValidateException;
use App\Repository\Contracts\FaqInterface;
use App\Repository\Contracts\FaqJavaInterface;
use App\Support\Helper\CommonHelper;
use Illuminate\Http\Request;
use Auth;

/**
 * @Author:: HuangYin
 * @DateTime: 2017-01-17 19:09
 *
 * @Description: 常见问题
 */
class FaqController extends Controller
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var FaqInterface
     */
    protected $faq;

    /**
     * @var FaqJavaInterface
     */
    protected $faqJava;

    protected $uid;

    /**
     * FaqController constructor.
     * @param Request $request
     * @param FaqInterface $faq
     * @param FaqJavaInterface $faqJava
     */
    public function __construct(Request $request, FaqInterface $faq, FaqJavaInterface $faqJava)
    {
        $this->request = $request;

        $this->faq = $faq;

        $this->faqJava = $faqJava;

        $this->uid = CommonHelper::getToken(Auth::user())['uid'];
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $limit = $this->request->input('limit', 15);
        $page = $this->request->input('page', 1);
        $list = $this->faq->lists($limit, $page);

        return $this->success($list);
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        $detail = $this->faq->detail($id);

        return $this->success($detail);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function syncList()
    {
        try {
            $maxId = $this->faq->findIdForFirst()?:0;
            $result = $this->faqJava->syncList($maxId);

            return $this->success($result);
        } catch (ValidateException $e) {

            return $this->validateError($e);
        }
    }
}